﻿using ZYCommon;
using System;
using System.Collections;
using System.Windows.Forms;
using TextEditor.TextDocument.Data;
using DevComponents.AdvTree;
using TextEditor.TextDocument.Data.Knowledge;
using System.Collections.Generic;
namespace TextEditor
{
   
    public class A_ContentChanged : A_Action
    {
        private bool isSelect = false;
        public A_ContentChanged()
        {
            base.HotKey = Keys.Control | Keys.Q;
        }

        public override string ActionName()
        {
            return AfterInsertCharActionName;
        }

        List<string> list = new List<string>();
        ZYTextChar preElement = null;
        public void GetSplitKeyWords(string preWord)
        {
            if (!string.IsNullOrEmpty(preWord))
            {
                string str = string.Empty;
                int i = preWord.Length;
                for (int x = i - 1; x >= 0; x--)
                {
                    str = preWord[x].ToString() + str;
                    list.Add(str);
                }
            }
        }

        public override bool Execute()
        {
            isSelect = false;
            list.Clear();
            preElement = base.myOwnerDocument.Content.PreElement as ZYTextChar;
            if ((preElement == null) || (preElement.Parent is ZYTextBlock))
            {
                return false;
            }
            string preWord = base.myOwnerDocument.Content.GetPreWord(preElement);
            if (preWord == null)
                return true;
            GetSplitKeyWords(preWord); 
            Singleton<KnowledgeBase>.Instance.showKnowledgeControl.OnInsertKnow = this.InsertKnow;
            Singleton<KnowledgeBase>.Instance.showKnowledgeControl.OnNotSelected = this.NotShow;
            Singleton<KnowledgeBase>.Instance.showKnowledgeControl.Owner = Bifrost.App.ParentForm;
            ShowKnowledge();
            return true;
        }

        public void ShowKnowledge()
        {
            if (!Singleton<KnowledgeBase>.Instance.GetKeyParam(list[0], this.OwnerDocument.OwnerControl.ViewPointToScreen(preElement.RealLeft, preElement.RealTop), preElement.Width, preElement.Height) && !isSelect && list.Count > 0)
            {
                if (list.Count > 0)
                {
                    list.RemoveAt(0);
                }
                if (list.Count == 0)
                    return;
                ShowKnowledge();
            }
        }

        public void InsertKnow(
            string value,
            string data, 
            string type,
            string name)
        {
            this.OwnerDocument._InsertKB(value, data, type == "100" ? "0" : "1",name);
            isSelect = true;
            this.list.Clear();
        }

        void NotShow()
        {
            if (list.Count > 1)
            {
                list.RemoveAt(0);
            }
            else
            {
                return;
            }
            ShowKnowledge();
        }

        public static string AfterInsertCharActionName
        {
            get
            {
                return "contentchanged";
            }
        }
    }
}

